package dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import dao.ComplaintAuditDao;
import po.ComplaintAudit;

@Repository("orderComplaintDao")
public class ComplaintAuditDaoImpl implements ComplaintAuditDao{

	@PersistenceContext(name="un")
	private EntityManager entityManager;
	
	@Override
	public List<ComplaintAudit> listAllOrderComplaint() {
		// TODO Auto-generated method stub
		String jpql = "from ComplaintAudit ca order by ca.status, ca.createDate desc";
		List<ComplaintAudit> orderComplaints = entityManager.createQuery(jpql).getResultList();
		return orderComplaints;
	}

	@Override
	@Transactional
	public String inserOrderComplaint(ComplaintAudit complaintAudit) {
		// TODO Auto-generated method stub
		entityManager.persist(complaintAudit);
		return complaintAudit.getoId();
	}

	@Override
	@Transactional
	public String updateComplaintStatus(ComplaintAudit complaintAudit) {
		// TODO Auto-generated method stub
		entityManager.merge(complaintAudit);
		return complaintAudit.getoId();
	}

	@Override
	public ComplaintAudit findComplaintById(String id) {
		// TODO Auto-generated method stub
		return entityManager.find(ComplaintAudit.class, id);
	}

	@Override
	public List<ComplaintAudit> listComplaintByMId(String mId) {
		// TODO Auto-generated method stub
		String jpql = "from ComplaintAudit ca where ca.mId = :mId";
		return entityManager.createQuery(jpql).setParameter("mId", mId).getResultList();
	}

	@Override
	public List<ComplaintAudit> listComplaintByCId(String cId) {
		// TODO Auto-generated method stub
		String jpql = "from ComplaintAudit ca where ca.cId = :cId";
		return entityManager.createQuery(jpql).setParameter("cId", cId).getResultList();
	}

}
